package com.aeye.mbr.upms.rpc.service.impl;

import com.aeye.mbr.cache.CacheService;
import com.aeye.mbr.common.result.BootstrapTableParams;
import com.aeye.mbr.common.result.BootstrapTableResult;
import com.aeye.mbr.common.util.MultiKeyMap;
import com.aeye.mbr.common.util.StringUtils;
import com.aeye.mbr.upms.common.constant.UpmsCacheConstants;
import com.aeye.mbr.upms.dao.model.UpmsArea;
import com.aeye.mbr.upms.dao.model.UpmsParam;
import com.aeye.mbr.upms.rpc.api.UpmsAreaService;
import com.aeye.mbr.upms.rpc.api.UpmsParamService;
import com.aeye.mbr.upms.rpc.mapper.UpmsAreaMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shengpeng on 2018/3/14.
 */
public class UpmsAreaCacheServiceImpl implements CacheService {

    @Autowired
    private UpmsAreaService upmsAreaService;

    @Override
    public String getCacheName() {
        return UpmsCacheConstants.UPMS_AREA_CACHE;
    }

    @Override
    public Object getCacheObject() {
        MultiKeyMap<String,UpmsArea> multiKeyMap = new MultiKeyMap();
        BootstrapTableParams params = new BootstrapTableParams();
        //全部查出来 TODO 需考虑dubbo序列化的最大值
        params.setLimit(Integer.MAX_VALUE);
        BootstrapTableResult<UpmsArea> result = upmsAreaService.selectUpmsAreaByParams(params,null);
        List<UpmsArea> list = result.getRows();
        for(UpmsArea area:list){
            String pid = area.getPid();
            if(StringUtils.isEmpty(pid)){
                pid = "0";
            }
            multiKeyMap.put(area.getId(),pid,area);
        }
        return multiKeyMap;
    }
}
